﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BEELab
{
    public partial class DuplicateStage : System.Web.UI.Page
    {
        /// <summary>
        /// Occures on every page load (first time or Post Back)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //if requested sebject ID not allowed to edit stage - return to main page
            if ( Request.QueryString["ID"] == null || (Request.QueryString["ID"] != null && BEELab.Logic.ProgramManagement.StageManagement.StageManagement.GetOwnerID(int.Parse(Request.QueryString["ID"].ToString())) != int.Parse(Session["UserID"].ToString()) && (Session["Admin"] == null || (bool)Session["Admin"] == false)))
            {
                Response.Redirect("Main.aspx");
            }

            //if it is the first time that the page is loaded
            if (!IsPostBack && Request.QueryString["ID"] != null)
            {
                //gets all Stages names exists in this experiment as a string seperated by '|'
                string Names = BEELab.Logic.ProgramManagement.StageManagement.StageManagement.EnumerateStagesForProgram(BEELab.Logic.ProgramManagement.StageManagement.StageManagement.GetProgramID(int.Parse(Request.QueryString["ID"].ToString())), "|");
                
                //if there is stages in this experiment 
                //sets the regular expression form that validates stages names
                if (Names.Length > 0)
                {
                    RegexStageName.ValidationExpression = "^(?!" + Names + ").*$";
                }
                //if there is no Stages - regular expression is empty (will except any form)
                else
                {
                    RegexStageName.ValidationExpression = ".*";
                }
            }
            //if it is not the first time that the page is loaded
            else
            {
                literal1.Text = "<script>parent.$.fancybox.close(); window.parent.location.reload();    </script>";
            }
        }

        /// <summary>
        /// Occures when the user press the submit button in order to duplucate existing stage
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button3_Click(object sender, EventArgs e)
        {
            //generates new id to the stage
            txtName.Text = txtName.Text.Replace("'", "").Replace("\"", "");
            int newID = BEELab.Logic.ProgramManagement.StageManagement.StageManagement.CreateStage(BEELab.Logic.ProgramManagement.StageManagement.StageManagement.GetProgramID(int.Parse(Request.QueryString["ID"].ToString())), txtName.Text, BEELab.Logic.ProgramManagement.StageManagement.StageManagement.GetDescription(int.Parse(Request.QueryString["ID"].ToString())), true, true, true);
            BEELab.Logic.ProgramManagement.StageManagement.StageManagement.ImportStageIntoStage(int.Parse(Request.QueryString["ID"].ToString()), newID, 0, "", false, false, new Dictionary<int,int>());
        }
    }
}